package com.ccsc.auth.service.impl;

import com.ccsc.auth.dao.SystemsDao;
import com.ccsc.auth.entity.Resource;
import com.ccsc.auth.entity.Systems;
import com.ccsc.auth.service.SystemsService;
import com.ccsc.auth.utils.DataMap;
import com.ccsc.auth.utils.Page;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by yyangs on 2017/2/15.
 */
@Service
public class SystemsServiceImpl  implements SystemsService{
    @Autowired
    private SystemsDao systemsDao;

    @Override
    public List<Systems> getAllSystemses() throws Exception {
        return systemsDao.getAllSystemsInfos();
    }

    @Override
    public List<Systems> getSystemsesByStatus(Integer Status) throws Exception {
        Systems systems=new Systems();
        systems.setStatus(Status);
        return systemsDao.getSystemsInfosByCondition(systems);
    }

    @Override
    public Page getSystemssByPage(DataMap params) throws Exception {
        List<Systems> resultList = systemsDao.getAllSystemsInfos(new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Systems> pageList = (PageList<Systems>) resultList;
        return new Page(pageList);
    }

    @Override
    public Page getSystemssByPage(Systems systems, DataMap params) throws Exception {
        List<Systems> resultList = systemsDao.getSystemsInfosByCondition(systems,new PageBounds(params.getInt("page"), params.getInt("pageSize")));
        PageList<Systems> pageList = (PageList<Systems>) resultList;
        return new Page(pageList);
    }

    @Override
    public Systems getSystemsById(Integer id) throws Exception {
        Systems systems=new Systems();
        systems.setId(id);
        return systemsDao.getSystemsById(systems);
    }

    @Override
    public Systems getSystemsByIdentifier(String identifier) throws Exception {
        Systems systems=new Systems();
        systems.setIdentifier(identifier);
        return systemsDao.getSystemsById(systems);
    }

    @Override
    public Systems getSystemsByResourceId(Integer resourceId) throws Exception {
        return systemsDao.getSystemsInfoByResourceId(resourceId);
    }

    @Override
    public Integer createSystems(Systems systems) throws Exception {
        return systemsDao.createSystems(systems);
    }

    @Override
    public Integer updateSystems(Systems systems) throws Exception {
        return systemsDao.updateSystems(systems);
    }

    @Override
    public Integer deleteSystemsById(Integer id) throws Exception {
        Systems systems=new Systems();
        systems.setId(id);
        return systemsDao.deleteSystems(systems);
    }

    @Override
    public Integer activeSystems(Integer id) throws Exception {
        Systems systems=new Systems();
        systems.setId(id);
        systems.setStatus(1);
        return systemsDao.updateSystems(systems);
    }

    @Override
    public Integer withdrawSystems(Integer id) throws Exception {
        Systems systems=new Systems();
        systems.setId(id);
        systems.setStatus(0);
        return systemsDao.updateSystems(systems);
    }

    @Override
    public Integer batchActiveSystems(List<Integer> ids) throws Exception {
        int result=0;
        for (Integer id:ids) {
            result=activeSystems(id);
        }
        return result;
    }

    @Override
    public Integer batchWithdrawSystems(List<Integer> ids) throws Exception {
        int result=0;
        for (Integer id:ids) {
            result=withdrawSystems(id);
        }
        return result;
    }
}
